﻿using System;
using System.Data;
using CardLogistics.Web.Components;
using System.Text;

namespace CardLogistics.Web.MainPage.Purchase.Demand
{
    public partial class ListInfo : BasePage
    {
        private static readonly CardLogistics.DAL.Purchase.Purchase_DemandData dal = new DAL.Purchase.Purchase_DemandData();
        protected void Page_Load(object sender, EventArgs e)
        {
            WebUserControl_ToolBar1.DeleteEvent += new EventHandler(WebUserControl_ToolBar1_DeleteEvent);
            if (!IsPostBack)
            {
                //状态 0 未提交 1 待审核 2 审核中 3 驳回 4 审核不通过 5审核通过
                BindDLL(ddlIsSubmit);
                BindRpt(LoadStrWhere);
            }
        }

        /// <summary>
        /// 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void WebUserControl_ToolBar1_DeleteEvent(object sender, EventArgs e)
        {
            string delId = this.hdf_Id.Value;
            if (!string.IsNullOrEmpty(delId))
            {
                string[] ids = delId.TrimEnd(',').Split(',');
                int resCount = 0;
                for (int i = 0; i < ids.Length; i++)
                {
                    string resStr = dal.Delete(ids[i]);
                    if (resStr.Split(',')[0] == "ok")
                    {
                        resCount++;
                        InsertLog("删除卡片需求单");
                    }
                }
                BindRpt("");
            }
        }

        /// <summary>
        /// 绑定Rpt数据
        /// </summary>
        private void BindRpt(string strWhere)
        {
            int recordCount = 0;
            int pageCount = 0;
            StringBuilder sbStr = new StringBuilder();
            sbStr.Append(" isdelete!= 1 ");
            if (txt_BillNo.Text.Trim().Length > 0)
                sbStr.Append(" and BillNO like '%" + txt_BillNo.Text.Trim().ToUpper() + "%' ");//单据编号
            if (txt_Department.Text.Trim().Length > 0)
                sbStr.Append(" and DEMANDDEPARTMENT like '%" + txt_Department.Text.Trim() + "%' ");//需求部门
            if (txt_StartDate.Text.Trim().Length > 0)
                sbStr.Append(" and DEMANDSTARTDATE>='" + txt_StartDate.Text.Trim() + "'");//需求开始日期
            if (txt_EndDate.Text.Trim().Length > 0)
                sbStr.Append(" and DEMANDENDDATE<='" + txt_EndDate.Text.Trim() + "'");//需求结束日期
            //审核状态
            if (ddlIsSubmit.SelectedValue != "-1")
            {
                sbStr.Append(" and IsSubmit=" + ddlIsSubmit.SelectedValue);
            }
            sbStr.Append(strWhere);
            DataTable dt = CardLogistics.DAL.CommonData.QuickPage_Oracle("v_purchase_demand", "ID", " issubmit,demandstartdate desc,billno  ", WebPageSize, WebPageIndex, sbStr.ToString(), "*", out pageCount, out recordCount);
            SetNetPagerHtml(AspNetPager1, dt, rptList, recordCount);
            #region 导出数据
            //导出语句
            string strExpSql = "select BILLNO 单据编号,DEMANDDEPARTMENT 需求部门,DEMANDSTARTDATE 需求开始日期,DEMANDENDDATE 需求结束日期,CardTotal 需求总张数,ConTotal 已购总张数,REMARKS 备注  from v_purchase_demand where" + sbStr.ToString();
            if (!Page.IsClientScriptBlockRegistered("regExportSql"))
                Page.RegisterClientScriptBlock("regExportSql", "<script language=\"Javascript\">var qsql=\"" + Server.UrlEncode(strExpSql) + "\";</script>");
            #endregion 
        }


        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            WebPageIndex = AspNetPager1.CurrentPageIndex;
            BindRpt("");
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Search_Click(object sender, EventArgs e)
        {
            BindRpt("");
        }
    }
}